# -*- encoding: utf-8 -*-
from storage.rawdata.es import ESRawEventDataStorage
from storage.rawdata import RawEventDataStorage
from storage.rawdata.es_bypass import ESBypassDataStorage


class BypassProxyRawDataStorage(RawEventDataStorage):
    def __init__(self, name):
        # self.fs_storage = LocalFileSystemRawEventDataStorage(name)
        self.es_cluster_storage = ESRawEventDataStorage(name, cluster='maestro')
        self.es_bypass_storage = ESBypassDataStorage(name, cluster='maestro')

    def save(self, events):
        # self.fs_storage.save(events)
        for event in events:
            # 2019-04-20 00:00:00 +08:00 之前的数据仍进入老的索引， 之后的索引按天分
            if event['_event_time'] < 1559073258:
                self.es_cluster_storage.save(event)
            else:
                self.es_bypass_storage.save(event)

    def add_batch(self, events):
        # self.fs_storage.add_batch(events)
        for event in events:
            # 2019-04-20 00:00:00 +08:00 之前的数据仍进入老的索引， 之后的索引按天分
            if event['_event_time'] < 1559073258:
                self.es_cluster_storage.add_batch(event)
            else:
                self.es_bypass_storage.add_batch(event)

    def execute_batch(self):
        # self.fs_storage.execute_batch()
        self.es_cluster_storage.execute_batch()
        self.es_bypass_storage.execute_batch()
